<php>if(!$isLoadScript){</php>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/ext/webuploader/webuploader.html5only.js"></script>
<link href="__PUBLIC__/ext/webuploader/webuploader.css" type="text/css" rel="stylesheet">
<php>}</php>

<div id="file_list_{$id}">
    <empty name="file">
        <else/><div>{$file.name} <a onclick="remove_file_{$id}(this,{$file.id})"><i class="icon-trash"></i></a> </div>
    </empty>

</div>

<div id="uploader_{$id}">
    <div class="btns">
        <div id="picker">{:L('_FILE_SELECT_')}</div>
    </div>
    <div class="progress progress-striped active" style="display: none;">
        <div class="progress-bar" role="progressbar" style="width: 100%;"></div>
    </div>
</div>
<input name="{$name}" id="file_upload_{$id}" type="hidden" value="{$value}">
<script>
    var id = "#uploader_{$id}";
    var $list_{$id}=$('#file_list_{$id}');
    var uploader_{$id} = WebUploader.create({

        // swf文件路径
        swf: 'Uploader.swf',

        // 文件接收服务端。
        server: "{:U('Core/File/uploadFile')}",

        // 选择文件的按钮。可选。
        // 内部根据当前运行是创建，可能是input元素，也可能是flash.
        pick: id,

        // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
        resize: false,

        fileSizeLimit: 200 * 1024 * 1024,    // 200 M
        fileSingleSizeLimit: 50 * 1024 * 1024    // 50 M
    });
    // 当有文件被添加进队列的时候
    uploader_{$id}.on( 'fileQueued', function( file ) {
        uploader_{$id}.upload();
    });
    // 文件上传过程中创建进度条实时显示。
    uploader_{$id}.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( "#uploader_{$id}" ),
                $percent = $li.find('.progress .progress-bar');
        // 避免重复创建
        if ( $percent.length == 1 ) {
            $percent = $('<div class="progress progress-striped active">' +
                    '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                    '</div>' +
                    '</div>').appendTo( $li ).find('.progress-bar');
        }
        $li.find('p.state').text('上传中');
        $percent.css( 'width', percentage * 100 + '%' );
    });

    uploader_{$id}.on( 'uploadSuccess', function( file ,ret ) {
        if(ret.status==1){
            toast.success("{:L('_SUCCESS_UPLOAD_')}{:L('_PERIOD_')}");
            $list_{$id}.html('<div>'+ret.data.file.name+' <a onclick="remove_file_{$id}'+'(this,'+ret.data.file.id+')"><i class="icon-trash"></i></a></div>');
           $('#file_upload_{$id}').val(ret.data.file.id);
        }else{
            toast.error("{:L('_FAIL_UPLOAD_')}{:L('_PERIOD_')}"+ret.info);
        }
        console.log(ret)
    });

    uploader_{$id}.on( 'uploadError', function( file ) {
        toast.error("{:L('_ERROR_UPLOAD_')}{:L('_PERIOD_')}")
    });

    uploader_{$id}.on( 'uploadComplete', function( file ) {
        $( "#uploader_{$id}" ).find('.progress').fadeOut();
    });

    function remove_file_{$id}(obj, attachId) {
        $('#file_upload_{$id}').val('');
        $(obj).parent('div').remove();
    }
</script>
